﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BHair.bb;
using DevExpress.XtraReports.UI;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.International.Formatters;
using Microsoft.International;
using System.Globalization;
using System.Diagnostics;

namespace BHair
{
    public partial class frmjinhuo : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmjinhuo()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void frmjinhuo_Load(object sender, EventArgs e)
        {
            this.label11.Text = null;
            string bs = "jh-";
            string time = DateTime.Now.ToString("yyyy-MM-dd");
            Random ran = new Random();
            int RandKey = ran.Next(10, 999);
            string djh = bs + time + RandKey.ToString();
            // MessageBox.Show(djh);
            this.lbbianhao.Text = djh;
            gys();
            jsr();
            cxsp();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            jinhuo.jinhuo.je = this.label11.Text;
            decimal sumje =decimal.Parse( this.label11.Text);
            jinhuo.jinhuo.dxje = InternationalNumericFormatter.FormatWithCulture("Lc",sumje,null,new CultureInfo("zh-CHT"));
            jinhuo.jinhuo.jhid = this.lbbianhao.Text;
            jinhuo.jinhuo.jsr = this.cbjsr.Text;
            jinhuo.jinhuo.gys = this.cbgys.Text;
            jinhuo.jinhuo.riqi = this.dateTimePicker1.Text;
            XtraReportjh jh = new XtraReportjh(dayin());
            jh.ShowPreviewDialog();
            
            
        }
        //供应商查询
        public void gys() 
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql1 = "SELECT [gysID],[gysname],[gysaddress],[gysphone],[gysbz] ,[gyslxr] FROM [BHairDB].[dbo].[gys]";
                SqlDataAdapter sda = new SqlDataAdapter(sql1,conn);
                DataSet ds = new DataSet();
                sda.Fill(ds,"gys");
                this.cbgys.DataSource = ds.Tables["gys"];
                this.cbgys.DisplayMember = "gysname";
                conn.Close();
            }
            catch (Exception ex) 
            {
                conn.Close();
                MessageBox.Show(ex.Message);
            }
        }
        //查询经手人
        public void jsr() 
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql1 = "SELECT [eid],[ecode] ,[ename],[esex],[ephone],[epid] ,[esalary],[ebonus],[einput],[eoutput],[estatus],[epassword],[eremark],[eaddress],[ecard],[emobile]FROM [BHairDB].[dbo].[employee]";
                SqlDataAdapter sda = new SqlDataAdapter(sql1,conn);
                DataSet ds = new DataSet();
                sda.Fill(ds, "employee");
                this.cbjsr.DataSource = ds.Tables["employee"];
                this.cbjsr.DisplayMember = "ename";
                conn.Close();
            }
            catch (Exception ex1) 
            {
                conn.Close();
                MessageBox.Show(ex1.Message);
            }
        }
        public void cxsp() 
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql = "SELECT [iid] as 商品编号,[iname] as 商品名称,round([iuprice],2) as 最近进价,[iuprice] as 预设售价,[iamount] as 商品库存 FROM [BHairDB].[dbo].[items]where istatus=0 and jc=0";
                SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                this.dataGridView1.DataSource = dt;
                conn.Close();

            }
            catch (Exception ex3) 
            {
                conn.Close();
                MessageBox.Show(ex3.Message);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            
            
            Frmgysadd gysadd = new Frmgysadd();
            gysadd.ShowDialog();
            gys();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.txtname.Text = this.dataGridView1.SelectedCells[1].Value.ToString();
            decimal jinhuo = decimal.Round(decimal.Parse(this.dataGridView1.SelectedCells[2].Value.ToString()), 2);    //此前余额
            this.txtprice.Text = jinhuo.ToString();
            this.lbbh.Text = this.dataGridView1.SelectedCells[0].Value.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int index = this.dataGridView2.Rows.Add();
            this.dataGridView2.Rows[index].Cells[0].Value = this.lbbh.Text;
            this.dataGridView2.Rows[index].Cells[1].Value = this.txtname.Text;
            this.dataGridView2.Rows[index].Cells[2].Value = this.txtprice.Text;
            this.dataGridView2.Rows[index].Cells[3].Value = this.txtnum.Text;
            this.dataGridView2.Rows[index].Cells[4].Value = decimal.Parse(this.txtnum.Text) * decimal.Parse(this.txtprice.Text);
            DataGridView dgv = dataGridView2;
            Total(dgv);
        }
        public void Total(DataGridView dg)
        {
           // DataGridViewRow dgr = dg.Rows[dg.Rows.Count];
            decimal sum = 0;
            for (int i = 0; i < dg.Rows.Count; i++)
            {
                sum +=Convert.ToDecimal( Convert.ToSingle(dg.Rows[i].Cells[4].Value));
            }
            this.label11.Text = sum.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string djh = this.lbbianhao.Text;
            string gys = this.cbgys.Text;
            string jsr = this.cbjsr.Text;
            string djbz = this.txtdjbz.Text;
            int rowcount = this.dataGridView2.Rows.Count;
            
            decimal kc = 0;
            SqlConnection conn1 = new SqlConnection(constr);

            //查询表格内商品的库存数量
            for (int i = 0; i < rowcount; i++)
            {
                string spID = this.dataGridView2.Rows[i].Cells[0].Value.ToString(); //商品编号
                string spname = this.dataGridView2.Rows[i].Cells[1].Value.ToString();//商品名称
                decimal jhdj =decimal.Parse( this.dataGridView2.Rows[i].Cells[2].Value.ToString());//进货价
                decimal jhnum =decimal.Parse( this.dataGridView2.Rows[i].Cells[3].Value.ToString());//进货数量
                decimal jhje = decimal.Parse(this.dataGridView2.Rows[i].Cells[4].Value.ToString());//进货金额
                string sql1 = string.Format("SELECT[iamount] FROM [BHairDB].[dbo].[items] where iid={0}", spID);
                try
                {
                    conn1.Open();
                    SqlCommand comm1 = new SqlCommand(sql1,conn1);
                    SqlDataReader sdr = comm1.ExecuteReader();
                    while (sdr.Read()) 
                    {
                        kc =decimal.Parse( sdr["iamount"].ToString().Trim());

                    }
                    conn1.Close();
                }
                catch(Exception ex2)
                {
                    conn1.Close();
                    MessageBox.Show(ex2.Message);
                }
                //修改商品表的库存数量
                SqlConnection conn2 = new SqlConnection(constr);
                try
                {
                    decimal upkc = kc + jhnum;//库存余量
                    conn2.Open();
                    //将本单 插入到 进货表
                    string sql2 = string.Format("INSERT INTO [BHairDB].[dbo].[jh]([djid],[spid],[spname],[spjhj] ,[spjhs],[spjhtime],[spgys],[spjsr] ,[spjhje],[spjhbz]) VALUES('{0}',{1},'{2}',{3},{4},'{5}','{6}','{7}',{8},'{9}')", djh, spID, spname, jhdj, jhnum, dateTimePicker1.Text, gys, jsr, jhje,djbz);
                    SqlCommand comm2 = new SqlCommand(sql2,conn2);
                    comm2.ExecuteNonQuery();
                    //修改库存数量
                    string sql3 = string.Format("UPDATE [BHairDB].[dbo].[items]SET [iamount] = {0} WHERE iid={1}", upkc,spID);
                    SqlCommand comm3 = new SqlCommand(sql3,conn2);
                    comm3.ExecuteNonQuery();

                }
                catch (Exception ex4) 
                {
                    MessageBox.Show(ex4.Message);
                }

            }
            MessageBox.Show("单据保存成功");
            this.button3.Enabled = true;
            
        }
        public DataSet dayin() 
        {
            DataSet ds = new DataSet();
            SqlConnection conn1 = new SqlConnection(constr);
            string sql1 = string.Format("SELECT [djid],[spid],[spname],[spjhj],[spjhs],[spjhtime],[spgys],[spjsr],[spjhje],[spjhbz] FROM [BHairDB].[dbo].[jh] where djid='{0}'",this.lbbianhao.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql1,conn1);
            sda.Fill(ds,"jh");


            return ds;
        }
    }
}
